package com.ty.system.service.impl;

import java.util.List;
import java.util.Random;

import com.ty.system.mapper.CoachMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ty.system.mapper.EnrollmentMapper;
import com.ty.system.domain.Enrollment;
import com.ty.system.service.IEnrollmentService;
import org.springframework.util.CollectionUtils;

/**
 * 驾校报名材料Service业务层处理
 *
 * @author
 * @date 2024-05-26
 */
@Service
public class EnrollmentServiceImpl implements IEnrollmentService
{
    @Autowired
    private EnrollmentMapper enrollmentMapper;
    @Autowired
    private CoachMapper coachMapper;

    /**
     * 查询驾校报名材料
     *
     * @param id 驾校报名材料主键
     * @return 驾校报名材料
     */
    @Override
    public Enrollment selectEnrollmentById(Long id)
    {
        return enrollmentMapper.selectEnrollmentById(id);
    }

    /**
     * 查询驾校报名材料列表
     *
     * @param enrollment 驾校报名材料
     * @return 驾校报名材料
     */
    @Override
    public List<Enrollment> selectEnrollmentList(Enrollment enrollment)
    {
        return enrollmentMapper.selectEnrollmentList(enrollment);
    }

    /**
     * 新增驾校报名材料
     *
     * @param enrollment 驾校报名材料
     * @return 结果
     */
    @Override
    public int insertEnrollment(Enrollment enrollment)
    {
        return enrollmentMapper.insertEnrollment(enrollment);
    }

    /**
     * 修改驾校报名材料
     *
     * @param enrollment 驾校报名材料
     * @return 结果
     */
    @Override
    public int updateEnrollment(Enrollment enrollment)
    {
        return enrollmentMapper.updateEnrollment(enrollment);
    }

    /**
     * 批量删除驾校报名材料
     *
     * @param ids 需要删除的驾校报名材料主键
     * @return 结果
     */
    @Override
    public int deleteEnrollmentByIds(Long[] ids)
    {
        return enrollmentMapper.deleteEnrollmentByIds(ids);
    }

    /**
     * 删除驾校报名材料信息
     *
     * @param id 驾校报名材料主键
     * @return 结果
     */
    @Override
    public int deleteEnrollmentById(Long id)
    {
        return enrollmentMapper.deleteEnrollmentById(id);
    }

    /**
     * 审核
     * @param enrollment 驾校报名材料
     * @return
     */
    @Override
    public int audit(Enrollment enrollment) {

        //审核通过直接分配教练
        if(enrollment.getState() == 1){
            List<Long> coachIds = coachMapper.selectCoachIds(enrollment.getLicenseType());
            //随机分配一个教练
            if(!CollectionUtils.isEmpty(coachIds)){
                Random random = new Random();
                int index = random.nextInt(coachIds.size());
                enrollment.setCoachId(coachIds.get(index));
            }
        }

        return enrollmentMapper.updateEnrollment(enrollment);
    }
}
